load JAW_81210_phase2TDS.mat  %load existing TDS
load JFM_92910_PC2.mat         %load PC file
load JFM_FSA_92910.mat             %load FSA file
A_AMA=PC_From_RAS(:,1:2)';  %use 1st and 2nd PCs
  A_mult=0.01; %A-matrix multiplier
OFFSET=[0; 0];
%OFFSET=OFFSETxy;
A=A_mult*A_AMA;

for i = 1 : 24
    glovesignals=Hc(i,1:19)';
    COORD(i,:) = A*glovesignals + OFFSET;
    XCOORD(i) = COORD(i,1); YCOORD(i) = COORD(i,2);
   
end
min_x=min(XCOORD);max_x=max(XCOORD);
mid_x=(min_x+max_x)/2; 
range_x=max_x-min_x;
min_y=min(YCOORD);max_y=max(YCOORD);
mid_y=(min_y+ max_y)/2;
range_y=max_y-min_y;

range_xy=(range_x+range_y)/2;
A_mult=A_mult*(5/range_xy)/0.6;
A=A_mult*A_AMA;
for i = 1 : 24
    glovesignals=Hc(i,1:19)';
    COORD(i,:) = A*glovesignals + OFFSET;
    XCOORD(i) = COORD(i,1); YCOORD(i) = COORD(i,2);  
end
min_x=min(XCOORD);max_x=max(XCOORD);mid_x=(min_x+max_x)/2; 
range_x=max_x-min_x;
min_y=min(YCOORD);max_y=max(YCOORD);mid_y=(min_y+ max_y)/2;
range_y=max_y-min_y;


OFFSETxy=[2.5-mid_x;  2.5-mid_y];
OFFSETx=[2.5-mid_x; 0.5];
OFFSETy=[0.5; 2.5-mid_y];
% OFFSETx=[-6.7; 0.5];
% OFFSETy=[0.5; 5.5];
% OFFSETxy=[-6.7; 5.5];
save JFM_92910_phase2TDS A_AMA A_mult OFFSETx OFFSETxy OFFSETy TARGETSx TARGETSxy TARGETSy subj_no Key_feedback;

 